/**
 * @file   GetClientNameResponsePacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-03-11
 * @brief  DataBus Register Client Request packet.
 *
 * Copyright (C) 2013 Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DataBus/Common/GetClientNameResponsePacket.h>
#include <DataBus/Common/Value.h>

bool DataBus::GetClientNameResponsePacket::create(const quint8 source,
                                                  const quint8 destination,
                                                  const quint8 packetId,
                                                  const QString &clientName,
                                                  Packet *packet)
{
    // Check parameters
    if ((source == 0) ||
        (packet == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Convert Client Name to UTF-8 encoding
    QByteArray name = clientName.toUtf8();

    if (name.size() > STRING_MAX_LENGTH)
    {
        // Error, binary representation of client name is too long
        return false;
    }

    // Create Header
    packet->setSource(source);
    packet->setDestination(destination);
    packet->setPacketType(PacketType_GetClientNameResponse);
    packet->setPacketId(packetId);

    // Create Payload
    QByteArray payload;

    payload.append(static_cast<char>(name.size()));
    payload.append(name);

    packet->setData(payload);

    // Success
    return true;
}

bool DataBus::GetClientNameResponsePacket::parse(const Packet &packet,
                                                 QString *clientName)
{
    // Check parameters
    if ((packet.getDestination() == 0) ||
        (packet.getPacketType() != PacketType_GetClientNameResponse) ||
        (clientName == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get data
    QByteArray data = packet.getData();

    if (data.size() < 1)
    {
        // Error, at least Length should be in the response
        return false;
    }

    // Get length
    int length = data.at(0);

    // Check if payload size is valid
    if (data.size() != (length + 1))
    {
        // Error, invalid length
        return false;
    }

    // Get Client Name
    if (Value::fromBinary(data, clientName) == false)
    {
        // Error, failed to parse client name
        return false;
    }

    // Success
    return true;
}
